package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketStorage;
import com.cskaoyan.market.db.domain.MarketStorageExample;
import com.cskaoyan.market.db.mapper.MarketStorageMapper;
import com.cskaoyan.market.service.AdminStorageService;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 刘国兴
 * @description 存储对象管理
 */

@Service
public class AdminStorageServiceImpl implements AdminStorageService {

    @Autowired
    MarketStorageMapper storageMapper;


    @Override
    public void insert(MarketStorage marketStorage) {
        storageMapper.insert(marketStorage);
    }

    @Override
    public void delete(MarketStorage marketStorage) {
        storageMapper.deleteByPrimaryKey(marketStorage.getId());
    }


    @Override
    public MarketStorage update(MarketStorage marketStorage) {
        MarketStorage storage = storageMapper.selectByPrimaryKey(marketStorage.getId());
        storage.setUpdateTime(LocalDateTime.now());
        storageMapper.updateByPrimaryKey(storage);
        return storage;
    }


    @Override
    public List<MarketStorage> list(String page, String limit, String sort, String order, String name, String key) {
        MarketStorageExample example = new MarketStorageExample();
        MarketStorageExample.Criteria criteria = example.createCriteria();


        if (StringUtils.isNotEmpty(key)) {
            criteria.andKeyEqualTo(key);
        }
        if (StringUtils.isNotEmpty(name)) {
            criteria.andNameLike("%"+name+"%");
        }

        example.setOrderByClause(sort+" "+order);

        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));

        List<MarketStorage> storageList = storageMapper.selectByExample(example);
        return storageList;
    }


}
